
Imports System
Imports System.Collections
Imports System.ComponentModel
Imports System.Data
Imports System.Drawing
Imports System.Web
Imports System.Web.SessionState
Imports System.Web.UI
Imports System.Web.UI.WebControls
Imports System.Web.UI.HtmlControls
Imports _2bMatesSite.BO

Namespace _2bMatesSite
	''' <summary>
	''' Summary description for confirm_join_group.
	''' </summary>
	Public Partial Class confirm_join_group
		Inherits BaseWebPage
		Private grpID As Integer = -1
		Protected htPageConstants As New Hashtable()

		Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs)
			' Auto login from an external link
			'AutoLogin( Request );

			' Put user code to initialize the page here
			' this.pageTemplate.PageTitle = PageTitles.JOIN_GROUP_CONFIRMATION;
			Dim usr As User = LoggedUser
			If usr = Nothing Then
				Common.Utils.RedirectToLoginPage(Page)
			End If

			' init page from XML
			Me.initPageTitles(Constants.CONFIRM_JOIN_GROUP, "", Form1, Nothing, htPageConstants)
			Me.initAdvertisements(Form1, Nothing)

			btnCancel.Attributes("onclick") = "javascript: history.back(); return false;"

			If Request.Params(Constants.GROUP_PARAM) = Nothing OrElse Request.Params(Constants.GROUP_PARAM) = "" Then
				Throw New Exception(Constants.ERROR_MESSAGE_MISSING_PARAMETERS)
			End If

			If Constants.SHOW_GROUPS <> Constants.SHOW_ON Then
				Throw New Exception(Constants.ERROR_MESSAGE_NO_ACCESS)
			End If

			Try
				grpID = Convert.ToInt32(Request.Params(Constants.GROUP_PARAM))
			Catch
			End Try

			Dim grp As Group = Groups.GetGroupByID(grpID)
			If grp = Nothing Then
				Throw New Exception(Constants.ERROR_MESSAGE_GROUP_DOESNT_EXIST_ERROR)
			End If

			If Request.Params(Constants.LOGIN_PARAM_USER) <> Nothing AndAlso Request.Params(Constants.LOGIN_PARAM_USER) <> "" Then
				' Page is opened from an email
				lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQAddGroup_TEXT")).Replace("[PARAM_1]", "<a href='" + Constants.GROUP + "?" + Constants.GROUP_PARAM + "=" + grp.ID + "'>")
				lblGroupMessage.Text += lblGroupMessage.Text.Replace("[PARAM_2]", grp.Name)
				lblGroupMessage.Text += lblGroupMessage.Text.Replace("[PARAM_2]", grp.Name)

				btnAccept.Visible = True
				btnReject.Visible = True
				btnJoinToGroup.Visible = False
				btnCancel.Visible = False
			Else
				lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgQSure_TEXT")).Replace("[PARAM]", grp.Name)
				btnAccept.Visible = False
				btnReject.Visible = False
				btnJoinToGroup.Visible = True
				btnCancel.Visible = True
			End If
		End Sub

		Protected Sub btnJoinToGroup_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			lblMainTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_JoinTitle_TEXT")
			Dim grp As Group = Groups.GetGroupByID(grpID)
			Dim user As User = Me.LoggedUser

			If grp.Members.Contains(user.ID) Then
				lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgAlreadyMember_TEXT")).Replace("[PARAM]", grp.Name)
				lblGroupMessage.Visible = True
				btnAccept.Visible = False
				btnReject.Visible = False
			Else
				If grp.Uid = user.ID Then
					lblGroupMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgModerator_TEXT")
				Else
					Dim grpPendingMembers As GroupsPendingUsers = grp.PendingMembers
					If Not grpPendingMembers.Contains(user.ID) Then
						grp.RequestMembership(user)
						If grp.PublicGroup Then
							lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgSuccessfullyJoined_TEXT")).Replace("[PARAM]", grp.Name)
						Else
							lblGroupMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgEmailSent_TEXT")
						End If
					Else
						lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgRequestAlreadySent_TEXT")).Replace("[PARAM]", grp.Name)
					End If
				End If
			End If

			btnCancel.Visible = False
			btnJoinToGroup.Visible = False
		End Sub

		Protected Sub btnReject_Click(ByVal sender As Object, ByVal e As System.EventArgs)
			lblMainTitle.Text = Me.GetPageConstText(htPageConstants, "CONST_JoinTitle_TEXT")

			Dim grp As Group = Groups.GetGroupByID(grpID)
			Dim user As User = Me.LoggedUser

			' Check if the user is already a group member when members functionality is done
			If grp.Members.Contains(user) Then
				lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgAlreadyMember_TEXT")).Replace("[PARAM]", grp.Name)
				lblGroupMessage.Visible = True
			Else
				If grp.Uid = user.ID Then
					lblGroupMessage.Text = Me.GetPageConstText(htPageConstants, "CONST_MsgModerator_TEXT")
				Else
					grp.CancelMembership(user.ID)
					lblGroupMessage.Text = (Me.GetPageConstText(htPageConstants, "CONST_MsgNoMembershipCreated_TEXT")).Replace("[PARAM]", user.Name)
				End If
			End If

			btnCancel.Visible = False
			btnJoinToGroup.Visible = False
			btnAccept.Visible = False
			btnReject.Visible = False
		End Sub

		#region Web Form Designer generated code
		Protected Overloads Overrides Sub OnInit(ByVal e As EventArgs)
			'
			' CODEGEN: This call is required by the ASP.NET Web Form Designer.
			'
			InitializeComponent()
			MyBase.OnInit(e)
		End Sub

		''' <summary>
		''' Required method for Designer support - do not modify
		''' the contents of this method with the code editor.
		''' </summary>
		Private Sub InitializeComponent()

		End Sub
#End Region

		====================
        ' This public property was added by conversion wizard to allow the access of a protected, autogenerated member variable Form1.
		====================
        Public ReadOnly Property Form1() As System.Web.UI.HtmlControls.HtmlForm
            Get
                Return Migrated_Form1
            End Get
        End Property
        'set { Migrated_Form1 = value; }
    End Class
End Namespace






